package com.hyb.algorithm.data.struct.backtrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 17. 电话号码的字母组合
 * https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
 * @author: ybhu
 * @create: 2023/12/25 17:30
 */
public class LetterCombinations {

    public static void main(String[] args) {

        String digits="23";

        List<String> ret=new LetterCombinations().letterCombinations(digits);

        int i=10;
    }


    public List<String> letterCombinations(String digits) {
        List<String> combinationsList = new ArrayList<String>();
        if (digits.length() == 0) {
            return combinationsList;
        }
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(combinationsList, phoneMap, digits, 0, new StringBuffer());
        return combinationsList;
    }

    public void backtrack(List<String> combinationsList, Map<Character, String> phoneMap, String digits, int index, StringBuffer stringBuffer) {
        if (index == digits.length()) {
            combinationsList.add(stringBuffer.toString());
        } else {
            char digit = digits.charAt(index);
            String letters = phoneMap.getOrDefault(digit,"");
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
                stringBuffer.append(letters.charAt(i));
                backtrack(combinationsList, phoneMap, digits, index + 1, stringBuffer);
                stringBuffer.deleteCharAt(index);
            }
        }
    }
}
